  /**
 *
 * Copyright 2016-present reading
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import React from 'react';
import {InteractionManager, NativeModules, StyleSheet, Image, Text, Linking, View, TouchableOpacity, Platform, ScrollView} from 'react-native';

import { createIconSetFromIcoMoon } from 'react-native-vector-icons';
import icoMoonConfig from '../../config/icoMoonConfig.json';
import colors from '../../config/colors';
const Icon = createIconSetFromIcoMoon(icoMoonConfig);
import Button from '../../components/Button';
import NavCell from '../../components/NavCell';

const SHOW_API = 'https://www.showapi.com';
const SCRM_REPO = 'https://github.com/attentiveness/reading';
import {RSA, RSAKeychain} from 'react-native-rsa-native';
var RNFS = require('react-native-fs');
import NavigationUtil from '../../utils/NavigationUtil';
const ReactAES = NativeModules.ReactAES;
const DeviceInfo = require('react-native-device-info');
import store from 'react-native-simple-store';
import Divider from '../../components/divider/Divider';
import { getEncryptedData } from '../../utils/TokenUtil';
const AESData = require('../../data/AE01.json');

import {
    MY_CARD,
    RED_PACK_RECORD
} from '../../constants/VueRoutes';

class Mine extends React.Component {

  onPress = (route) => {
    const { navigate } = this.props.navigation;
    navigate(route.name, route.args);
  };

  constructor(props) {
      super(props);
      this.onLogout = this.onLogout.bind(this);
      this.navigateToLink = this.navigateToLink.bind(this);
      this.state = {
          isManager: false,
          isTeamManager: false
      }
  }

  componentDidMount () {
    const { userActions } = this.props;
    InteractionManager.runAfterInteractions(() => {
        store.get('token')
          .then(token => {
            userActions.requestUser(token)
          })
      });
  }

  async go (link) {
    try {
      const { navigate } = this.props.navigation;
      const url = await link();
      navigate('scrm', {
        article: {
          url: url,
          title: '',
        }
      });
    } catch(err) {
      console.log(err, 'err')
    }
  }

  navigateToLink (route) {
    this.go(route)
  };


  onLogout () {
    const { userActions, teamActions } = this.props;
    store.delete('authData');
    store.delete('site_id');
    store.delete('token');
    store.delete('my_team_list');
    userActions.receiveUser(null);
    teamActions.emptyTeamList(null, false);
    teamActions.resetTeamType();
    NavigationUtil.reset(this.props.navigation, 'Login');
  }


  render() {
        const { user } = this.props.user;
        console.log('test user', JSON.stringify(user))
        let auth = {};
        if (user) {
            auth = {
                isManager: !!user.is_manager,
                isTeamManager: !!user.is_team_manager
            }
        }
        return (
            <View style={styles.container}>
                {/*导航条*/}
                <ScrollView>
                    <View style={{marginTop:20}}>
                        <Divider />
                        <NavCell
                          title="我的名片"
                          route={MY_CARD}
                          onPressHandler={this.navigateToLink}
                        />
                        <Divider />
                    </View>
                    { (auth.isManager || auth.isTeamManager) &&
                        <View style={{marginTop:20}}>
                            { auth.isManager &&
                                <Divider />
                            }
                            { auth.isManager &&
                                <NavCell
                                title="团队设置"
                                route={{name:'TeamSetting', args:{}}}
                                onPressHandler={this.onPress}
                                />
                            }
                            { auth.isTeamManager &&
                                <Divider />
                            }
                            { auth.isTeamManager &&
                                <NavCell
                                    route={{name:'MyTeam', args:{isOwn: true}}}
                                    onPressHandler={this.onPress}
                                    title="我的团队设置"
                                />
                            }
                            <Divider />
                        </View>
                    }
                    <View style={{marginTop:20}}>
                        <Divider />
                        <NavCell
                            title="红包记录"
                            route={RED_PACK_RECORD}
                            onPressHandler={this.navigateToLink}
                        />
                        <Divider />
                    </View>

                    <View style={{marginTop:20}}>
                        <Divider />
                        <NavCell
                            containerStyle={styles.loginNavStyle}
                            isShowArrow={false}
                            title="退出登陆"
                            onPressHandler={this.onLogout}
                        />
                        <Divider />
                    </View>

                </ScrollView>
            </View>
        );
    };
}

const styles = StyleSheet.create({
    // 导航条视图
    navOutViewStyle:{
        height:Platform.OS === 'ios' ? 64 : 44,
        backgroundColor: 'rgba(255,96,0,1)',
        // 主轴方向
        flexDirection:'row',
        // 侧轴对齐方式 垂直居中
        alignItems:'center',
        // 主轴方向居中
        justifyContent:'center',
    },
    // 导航栏右侧
    rightViewStyle:{
        // 绝对定位
        position:'absolute',
        right:10,
        bottom:15,
    },
    // 导航条上图片
    navImgStyle:{
        width:Platform.OS === 'ios' ? 28 : 24,
        height:Platform.OS === 'ios' ? 28 : 24,
    },
    container: {
        flex: 1,
        backgroundColor: colors.grey9,
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    logoutBtnStyle: {
        textAlign: 'center',
        flex: 1,
        padding: 10,
        backgroundColor: '#da3f3e',
        color: 'white',
        marginLeft: 120,
        marginRight: 120,
        marginTop: 100,
        borderRadius: 20
    },
    loginNavStyle: {
        justifyContent: 'center'
    }

});

export default Mine;
